package com.cn.aam.checaiduo.center;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.bean.UpdateInfoB;
import com.cn.aam.checaiduo.minterface.MyUMShareListener;
import com.cn.aam.checaiduo.utils.IntentUtils;
import com.cn.aam.checaiduo.utils.SpannableUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;

import static android.app.Activity.RESULT_CANCELED;


/**
 * 自定义通知栏管理中心
 */
public class CustomDialogCenter {


    private CustomDialogCenter() {
    }

    public static CustomDialogCenter getInstance() {
        return CustomDialogCenter.Holder.INSTANCE;
    }

    private static class Holder {
        static CustomDialogCenter INSTANCE = new CustomDialogCenter();

    }


    /**
     * @param mContext   上下文
     * @param content    提示的内容
     * @param leftStr    左按钮文本
     * @param rightStr   右按钮文本
     * @param onTipClick 左右按钮监听者
     * @description 通用弹窗  -提示操作
     */
    public void showTipsDialog(final Context mContext, String content, String leftStr, String rightStr, final OnTipClick onTipClick) {
        //dialog
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_universal_tip, false)
                .show();
        final View customView = dialog.getCustomView();
        //alpha animation
//        showWithAnim(mContext, customView);

        //content
        TextView tvContent = (TextView) customView.findViewById(R.id.tv_content);
        tvContent.setText(content);
        //btn
        TextView tvLeft = (TextView) customView.findViewById(R.id.btn_left);
        if (TextUtils.isEmpty(leftStr)) {
            tvLeft.setVisibility(View.GONE);
        }
        tvLeft.setText(leftStr);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onTipClick != null)
                    onTipClick.onLeftButtonClick();
            }
        });

        TextView tvRight = (TextView) customView.findViewById(R.id.btn_right);
        tvRight.setText(rightStr);
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onTipClick != null)
                    onTipClick.onRightButtonClick();
            }
        });

    }

    /**
     * 展示权限提示弹窗-模板
     *
     * @param context
     * @param title
     */
    public void showPermissionTipDialog(final Context context, String title) {
        showTipsDialog(context, title, "知道了", "添加权限",
                new CustomDialogCenter.OnTipClick() {
                    @Override
                    public void onLeftButtonClick() {

                    }

                    @Override
                    public void onRightButtonClick() {
                        IntentUtils.openMyAppInfo(context);
                    }
                });
    }

    /**
     * 进场动画
     */
    private void showWithAnim(final Context mContext, final View customView) {
        customView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.dialog_show);
                customView.startAnimation(animation);
            }
        }, 30);

    }

    /**
     * 离场动画
     */
    private void dismissWithAnim(Context mContext, View customView, final MaterialDialog dialog, final CallBack callBack) {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.dialog_dismiss);
        customView.startAnimation(animation);
        customView.postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
                if (callBack != null)
                    callBack.onCall();
            }
        }, 400);
    }


    /**
     * @param mContext 上下文
     * @description 实名认证成功
     */
    public void showAuthSuccessDialog(final Context mContext) {
        //dialog
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_auth_success, false)
                .show();
        View customView = dialog.getCustomView();

        TextView tvBack = (TextView) customView.findViewById(R.id.btn_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                AppData.closeActivityExceptMain();
            }
        });

    }

    /**
     * 备注：修改实名认证信息-手机号码
     * 备注:change mobile success
     */
    public void showCMSDialog(final Context mContext, final CallBack callBack) {
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_change_mobile_success, false)
                .show();
        View customView = dialog.getCustomView();
        customView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (callBack != null)
                    callBack.onCall();
            }
        });
    }

    /**
     * @param mContext 上下文
     * @description 新手乐园
     */
    public void showExitParkDialog(final Context mContext) {
        //dialog
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_park_exit, false)
                .show();
        View customView = dialog.getCustomView();

        TextView tvBack = (TextView) customView.findViewById(R.id.btn_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ((BaseActivity) mContext).setResult(RESULT_CANCELED);
                ((BaseActivity) mContext).overridePendingTransition(0, R.anim.slide_out_from_bottom);
                ((BaseActivity) mContext).finish();
            }
        });

        customView.findViewById(R.id.btn_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }


    /**
     * @param mContext 上下文
     * @description 注册界面
     */
    public void showExitRegisterDialog(final Context mContext, final CallBack callBack) {
        //dialog
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_register_exit, false)
                .show();
        View customView = dialog.getCustomView();

        TextView tvContent = (TextView) customView.findViewById(R.id.tv_content);
        SpannableString spanString = new SpannableString("可以获得188元现金红包哦~");
        spanString.setSpan(new AbsoluteSizeSpan(30, true), 4, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(spanString);

        TextView tvBack = (TextView) customView.findViewById(R.id.btn_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (callBack != null) callBack.onCall();
            }
        });

        customView.findViewById(R.id.btn_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }


    /**
     * @param mContext 上下文
     * @description 更新弹窗
     */
    public void showAppUpdate(final Context mContext, final UpdateInfoB updateInfoB) {
        //dialog
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_app_update, false)
                .show();
        View customView = dialog.getCustomView();

        TextView tvTip = (TextView) customView.findViewById(R.id.tv_tip);

        //分解描述内容
        String[] split = updateInfoB.getDescription().split("；");
        String target = "";
        for (int i = 0; i < split.length; i++) {
            if (i == split.length - 1) {
                target += split[i];
                break;
            }
            target += split[i] + "\n";
        }
        tvTip.setText(target);

        //点击事件
        customView.findViewById(R.id.btn_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                IntentUtils.openWebSite(updateInfoB.getDownload_url());
            }
        });

        customView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    /**
     * @param mContext 上下文
     * @description 分享
     */
    public void showShareDialog(final Activity mContext, final UMWeb web) {
        //dialog
        final MaterialDialog dialog = new MaterialDialog.Builder(mContext)
                .cancelable(false)
                .backgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                .customView(R.layout.dialog_share, false)
                .show();
        View customView = dialog.getCustomView();
        customView.findViewById(R.id.share_weichat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                new ShareAction(mContext).setPlatform(SHARE_MEDIA.WEIXIN)
                        .withMedia(web)
                        .setCallback(new MyUMShareListener())
                        .share();
            }
        });

        customView.findViewById(R.id.share_qq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                new ShareAction(mContext).setPlatform(SHARE_MEDIA.QQ)
                        .withMedia(web)
                        .setCallback(new MyUMShareListener())
                        .share();
            }
        });

        customView.findViewById(R.id.share_sina).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                new ShareAction(mContext).setPlatform(SHARE_MEDIA.SINA)
                        .withMedia(web)
                        .setCallback(new MyUMShareListener())
                        .share();
            }
        });
    }

    public interface CallBack {
        void onCall();
    }


    public interface OnTipClick {
        void onLeftButtonClick();

        void onRightButtonClick();
    }

}
